// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "名称",
    "label": "名称"
  },
  "category_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "分类ID",
    "label": "分类ID"
  },
  "original_good_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "原商品ID",
    "label": "原商品ID"
  },
  "goods_thumb": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^(http://|https://|/|./|@/)\\S"
      }
    ],
    "title": "缩略图地址",
    "label": "缩略图地址"
  },
  "merchant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "转售商户ID",
    "label": "转售商户ID"
  },
  "remain_count": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "库存数量",
    "label": "库存数量"
  },
  "price": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "转售价格",
    "label": "转售价格"
  },
  "original_price": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "原购买价格",
    "label": "原购买价格"
  },
  "keywords": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "关键字",
    "label": "关键字"
  },
  "condition_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "全新未拆",
            "value": 0
          },
          {
            "text": "全新仅拆",
            "value": 1
          },
          {
            "text": "9.9成新",
            "value": 2
          },
          {
            "text": "9成新",
            "value": 3
          },
          {
            "text": "8成新",
            "value": 4
          },
          {
            "text": "7成新",
            "value": 5
          },
          {
            "text": "6成新",
            "value": 6
          },
          {
            "text": "5成新",
            "value": 7
          },
          {
            "text": "4成新",
            "value": 8
          },
          {
            "text": "3成新",
            "value": 9
          },
          {
            "text": "2成新",
            "value": 10
          },
          {
            "text": "1成新",
            "value": 11
          }
        ]
      }
    ],
    "title": "商品成色",
    "label": "商品成色"
  },
  "resale_reason": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "转售原因",
    "label": "转售原因"
  },
  "condition_desc": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "成色描述",
    "label": "成色描述"
  },
  "resale_imgs": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "转售实拍图",
    "label": "转售实拍图"
  },
  "favorite_count": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "收藏次数",
    "defaultValue": 0,
    "label": "收藏次数"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "在售",
            "value": 0
          },
          {
            "text": "售罄",
            "value": 1
          },
          {
            "text": "已下架",
            "value": 2
          }
        ]
      }
    ],
    "title": "转售状态",
    "defaultValue": 0,
    "label": "转售状态"
  },
  "add_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "上架时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "上架时间"
  },
  "postfee_template_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "邮费模板ID",
    "label": "邮费模板ID"
  },
  "free_shipping": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否包邮",
    "defaultValue": false,
    "label": "是否包邮"
  },
  "weight": {
    "rules": [
      {
        "format": "number"
      }
    ],
    "title": "商品重量",
    "label": "商品重量"
  },
  "seller_note": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "卖家备注",
    "label": "卖家备注"
  },
  "type": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "售卖类型",
    "defaultValue": 1,
    "label": "售卖类型"
  },
  "hashcode": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "上链哈希码",
    "label": "上链哈希码"
  }
}

const enumConverter = {
  "condition_status_valuetotext": {
    "0": "全新未拆",
    "1": "全新仅拆",
    "2": "9.9成新",
    "3": "9成新",
    "4": "8成新",
    "5": "7成新",
    "6": "6成新",
    "7": "5成新",
    "8": "4成新",
    "9": "3成新",
    "10": "2成新",
    "11": "1成新"
  },
  "status_valuetotext": {
    "0": "在售",
    "1": "售罄",
    "2": "已下架"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
